package com.tqw.client.controller;

import com.tqw.client.pojo.ImgResult;
import com.tqw.client.service.ClientFileService;
import com.tqw.client.vo.ClientVO;
import com.tqw.client.vo.ImageDTO;
import com.tqw.constant.SecurityConstants;
import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

import static com.tqw.client.pojo.Code.SUCCESS;
import static com.tqw.entity.StatusCode.ERROR;
import static com.tqw.entity.StatusCode.OK;

/**
 * 头像上传
 * @author Will
 */
@RestController
@RequestMapping("/client/avatar")
public class ClientFileController {

    @Autowired
    private ClientFileService clientFileService;

    /**
     * 上传头像
     * @param img
     * @return
     */
    @PostMapping("/upload")
    public Result<ImageVO> fileUpload(MultipartFile img, @RequestHeader(SecurityConstants.AUTH_TOKEN)String token){
        ImgResult imgResult = clientFileService.upload(img, token);
        //成功
        if (imgResult.getCode() == 300){
            ImageDTO imageDTO = new ImageDTO(imgResult.getImageVO(),imgResult.getToken());
            String resultMessage = imgResult.getResultMessage();
            return new Result<>(true, OK, resultMessage, imageDTO);
        }
        //错误
        String resultMessage = imgResult.getResultMessage();
        return new Result<>(false, ERROR, resultMessage);


    }

}
